package com.goodving.guli.bt.weight.family;

import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.activity.WeightGoalSetActivity;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.C1154e;
import com.goodving.guli.bt.DimPanelFragment;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventUserDeleted;
import com.goodving.guli.bt.eventbus.EventUserInfoUpdate;
import com.goodving.guli.bt.lua.C1226h;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.ui.C1795p;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.bt.weight.WeightGoalManager;
import com.goodving.guli.fragment.AgePanelFragment;
import com.goodving.guli.fragment.HeightPanelFragment;
import com.goodving.guli.fragment.NickNamePanelFragment;
import com.goodving.guli.fragment.PersonIconPanelFragment;
import com.goodving.guli.fragment.WeightPanelFragment;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.C3247b;

import java.io.ByteArrayOutputStream;
import java.io.File;

import de.greenrobot.event.EventBus;

/* renamed from: cn.com.smartdevices.bracelet.weight.family.h */
public class UserInfoFragment extends C1795p implements OnClickListener { // C2023h
    public static final int f11476b = 17;
    public static final int f11477c = 18;
    public static final int f11478d = 273;
    public static final String f11479e = "EXTRA_USER_INFO_UID";
    private static final String TAG = "UserInfoFragment";
    private UserInfo mUserInfo;
    private Birthday mBirthday;
    private TextView genderValueTv;
    private View genderView;
    private TextView heightValueTv;
    private View heightView;
    private TextView weightValueTv;
    private View weightView;
    private TextView ageValueTv;
    private View ageView;
    private View userInfoAreaView;
    private ImageButton pageIconBut;
    private TextView pageNickNameTv;
    private ImageButton editMemberNameBut;
    private TextView pageFigureTv;
    private Bitmap f11496v;
    private TextView pageBmiTv;
    private LinearLayout setGoalLayout;
    private TextView setGoalTv;

    public UserInfoFragment() {
        this.mUserInfo = null;
    }

    public static Fragment m15865a(int i) {
        Fragment c2023h = new UserInfoFragment();
        Bundle bundle = new Bundle();
        if (i >= 0) {
            bundle.putInt(f11479e, i);
        }
        c2023h.setArguments(bundle);
        return c2023h;
    }

    private void initView(View view) {
        this.genderValueTv = (TextView) view.findViewById(R.id.info_gender_value);
        this.genderView = view.findViewById(R.id.info_gender);
        this.genderView.setOnClickListener(this);
        this.heightValueTv = (TextView) view.findViewById(R.id.info_height_value);
        this.heightView = view.findViewById(R.id.info_height);
        this.heightView.setOnClickListener(this);
        this.weightValueTv = (TextView) view.findViewById(R.id.info_weight_value);
        this.weightView = view.findViewById(R.id.info_weight);
        this.weightView.setOnClickListener(this);
        this.ageValueTv = (TextView) view.findViewById(R.id.info_age_value);
        this.ageView = view.findViewById(R.id.info_age_area);
        this.ageView.setOnClickListener(this);
        this.setGoalLayout = (LinearLayout) view.findViewById(R.id.device_set_goal);
        this.setGoalLayout.setOnClickListener(this);
        this.setGoalTv = (TextView) view.findViewById(R.id.device_set_goal_text);
        view.findViewById(R.id.del_member).setOnClickListener(this);
        this.userInfoAreaView = view.findViewById(R.id.member_info_user_info_area);
        initUserInfoAreaView();
    }

    private void initUserInfoAreaView() {
        this.pageIconBut = (ImageButton) this.userInfoAreaView.findViewById(R.id.member_page_icon);
        this.pageIconBut.setOnClickListener(this);
        this.pageNickNameTv = (TextView) this.userInfoAreaView.findViewById(R.id.member_page_nickname);
        this.pageNickNameTv.setOnClickListener(this);
        this.editMemberNameBut = (ImageButton) this.userInfoAreaView.findViewById(R.id.edit_member_name);
        this.editMemberNameBut.setOnClickListener(this);
        this.pageFigureTv = (TextView) this.userInfoAreaView.findViewById(R.id.member_page_figure);
        this.pageBmiTv = (TextView) this.userInfoAreaView.findViewById(R.id.member_page_bmi);
    }

    private void m15870d() {
        if (getActivity() == null) {
            GuliLog.tracePrintError(TAG, "getActivity is null");
            return;
        }
        this.genderValueTv.setText(this.mUserInfo.gender == 1 ? getString(R.string.male) : getString(R.string.female));
        this.heightValueTv.setText(m15871e());
        if (this.mUserInfo.weight > 0.0f && this.mUserInfo.height > 0) {
            this.weightValueTv.setText(WeightUtil.m15706b(this.mUserInfo.weight, Keeper.readPersonInfo().miliConfig.weightUnit) + WeightUtil.m15698a(getActivity().getApplicationContext(), Keeper.readPersonInfo().miliConfig.weightUnit));
            float a = WeightUtil.m15693a(this.mUserInfo.height, Birthday.fromStr(this.mUserInfo.birthday).getAge(), this.mUserInfo.weight);
            this.pageBmiTv.setText(WeightUtil.m15709b(a));
            this.pageFigureTv.setText(WeightUtil.m15697a(getActivity(), a, Birthday.fromStr(this.mUserInfo.birthday).getAge(), this.mUserInfo.gender));
        }
        this.mBirthday = Birthday.fromStr(this.mUserInfo.birthday);
        if (this.mBirthday != null) {
            this.ageValueTv.setText(this.mBirthday.toString());
        }
        Utils.m8042b(this.mUserInfo, this.pageIconBut);
        this.pageNickNameTv.setText(this.mUserInfo.name);
        if (this.mUserInfo.targetWeight > 0.0f) {
            this.setGoalTv.setText(((int) WeightUtil.m15706b(this.mUserInfo.targetWeight, Keeper.readPersonInfo().miliConfig.weightUnit)) + WeightUtil.m15698a(getActivity().getApplicationContext(), Keeper.readPersonInfo().miliConfig.weightUnit));
        } else {
            this.setGoalTv.setText(getString(R.string.not_set));
        }
    }

    private String m15871e() {
        int i = this.mUserInfo.height;
        int a = Utils.m7962a(i);
        if (Keeper.readPersonInfo().getUnit() == 0) {
            return i + getString(R.string.cm);
        }
        String quantityString = getResources().getQuantityString(R.plurals.numberFoot, a / 12, new Object[]{Integer.valueOf(a / 12)});
        return quantityString + " " + getResources().getQuantityString(R.plurals.numberInch, a % 12, new Object[]{Integer.valueOf(a % 12)});
    }

    private void m15872f() {
        fr frVar = new fr();
        Bundle bundle = new Bundle();
        bundle.putString(C1841l.f10280a, getString(R.string.del_family_member));
        bundle.putString(C1841l.f10281b, getString(R.string.del_family_member_content));
        bundle.putString(C1841l.f10283d, getString(R.string.delete));
        bundle.putString(C1841l.f10282c, getString(R.string.cancel));
        frVar.setArguments(bundle);
        frVar.setOpClickListener(new C2024i(this));
        DimPanelFragment.showPanel(getActivity(), frVar);
    }

    private boolean m15873g() {
        UserInfoManager.getInstance().deleteInfoMark(this.mUserInfo);
        GuliLog.tracePrintInfo(TAG, "after deleteInfoMark " + this.mUserInfo.toString());
        return UserInfoManager.getInstance().syncWithServer(getActivity().getApplicationContext());
    }

    @SuppressLint({"NewApi"})
    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == f11476b && intent != null) {
            GuliLog.tracePrintInfo(TAG, "receive select img fragment with uid " + this.mUserInfo.uid + " uid name " + this.mUserInfo.name + " in crop img");
            String stringExtra = intent.getStringExtra("CROPPED_FILE_PATH");
            Bitmap a = C1226h.m11372a().m11375a(Uri.fromFile(new File(stringExtra)).toString());
            if (a != null) {
                try {
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    a.compress(CompressFormat.PNG, 100, byteArrayOutputStream);
                    this.mUserInfo.avatarSource = byteArrayOutputStream.toByteArray();
                    this.mUserInfo.avatarUrl = null;
                    GuliLog.tracePrintInfo(TAG, "save avatar source from select pic" + this.mUserInfo.avatarSource);
                    a.recycle();
                    GuliLog.tracePrintInfo(TAG, "onActivityResult requst_code =" + i + " task photo post evnetbus");
                    EventBus.getDefault().post(new EventUserInfoUpdate(this.mUserInfo));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            GuliLog.tracePrintInfo(TAG, "the crop image file path is " + stringExtra);
        } else if (i == f11477c && intent != null && intent.getExtras() != null) {
            GuliLog.tracePrintInfo(TAG, "receive select img fragment with uid " + this.mUserInfo.uid + " uid name " + this.mUserInfo.name + " in take photo");
            this.f11496v = (Bitmap) intent.getExtras().get("data");
            if (this.f11496v != null) {
                try {
                    ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
                    this.f11496v.compress(CompressFormat.PNG, 100, byteArrayOutputStream2);
                    this.mUserInfo.avatarSource = byteArrayOutputStream2.toByteArray();
                    GuliLog.tracePrintInfo(TAG, "save avatar source from take photo" + this.mUserInfo.avatarSource);
                    this.f11496v.recycle();
                    this.f11496v = null;
                    GuliLog.tracePrintInfo(TAG, "onActivityResult requst_code =" + i + " task photo post evnetbus");
                    EventBus.getDefault().post(new EventUserInfoUpdate(this.mUserInfo));
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        } else if (f11478d != i) {
        } else {
            if (WeightGoalSetActivity.f11180c == i2) {
                EventBus.getDefault().post(new EventUserInfoUpdate(this.mUserInfo));
            } else if (WeightGoalSetActivity.f11181d == i2) {
                float floatExtra = intent.getFloatExtra(WeightGoalSetActivity.f11179b, 0.0f);
                this.mUserInfo.targetWeight = WeightUtil.m15689a(floatExtra, Keeper.readPersonInfo().miliConfig.weightUnit);
                EventBus.getDefault().post(new EventUserInfoUpdate(this.mUserInfo));
            }
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.info_gender:
                C0668a.m8126a(getActivity(), C0668a.fu);
                DimPanelFragment.showPanel(getActivity(), dd.m14648a(this.mUserInfo.uid).getClass());
                break;
            case R.id.info_age_area:
                C0668a.m8126a(getActivity(), C0668a.fw);
                DimPanelFragment.showPanel(getActivity(), AgePanelFragment.m14642a(this.mUserInfo.uid).getClass());
                break;
            case R.id.info_height:
                C0668a.m8126a(getActivity(), C0668a.fv);
                DimPanelFragment.showPanel(getActivity(), HeightPanelFragment.m14650a(this.mUserInfo.uid).getClass());
                break;
            case R.id.info_weight:
                DimPanelFragment.showPanel(getActivity(), WeightPanelFragment.m14666a(this.mUserInfo.uid).getClass());
                break;
            case R.id.device_set_goal:
                Intent intent = new Intent(getActivity(), WeightGoalSetActivity.class);
                intent.putExtra(WeightGoalSetActivity.f11178a, this.mUserInfo.uid);
                getActivity().startActivityForResult(intent, f11478d);
                C0668a.m8126a(getActivity(), C0668a.fy);
                break;
            case R.id.del_member:
                C0668a.m8126a(getActivity(), C0668a.fq);
                m15872f();
                break;
            case R.id.member_page_icon:
                C0668a.m8126a(getActivity(), C0668a.fx);
                GuliLog.tracePrintInfo(TAG, "open select img fragment with uid " + this.mUserInfo.uid + " uid name " + this.mUserInfo.name);
                DimPanelFragment.showPanel(getActivity(), PersonIconPanelFragment.class);
                break;
            case R.id.member_page_nickname:
            case R.id.edit_member_name:
                C0668a.m8126a(getActivity(), C0668a.ft);
                DimPanelFragment.showPanel(getActivity(), NickNamePanelFragment.m14664a(this.mUserInfo.uid).getClass());
                break;
            default:
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.mUserInfo = UserInfoManager.getInstance().getInfoByUid(getArguments().getInt(f11479e));
        GuliLog.tracePrintInfo(TAG, "receive userinfo =" + this.mUserInfo);
        EventBus.getDefault().register(this);
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.member_info_fragment, null);
        initView(inflate);
        m15870d();
        return inflate;
    }

    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onDestroyView() {
        super.onDestroyView();
        if (this.mUserInfo != null) {
            GuliLog.tracePrintInfo(TAG, "onDestroyView here save info " + this.mUserInfo.toString());
        }
        UserInfoManager.getInstance().m15599d(this.mUserInfo);
    }

    public void onEvent(EventUserInfoUpdate eventUserInfoUpdate) {
        if (eventUserInfoUpdate != null && eventUserInfoUpdate.getInfo() != null) {
            this.mUserInfo = eventUserInfoUpdate.getInfo();
            GuliLog.tracePrintInfo(TAG, "update ... ui " + this.mUserInfo);
            UserInfoManager.getInstance().m15599d(this.mUserInfo);
            m15870d();
        }
    }

    public void onPause() {
        super.onPause();
        C0668a.m8133a(C0668a.aW);
    }

    public void onStop() {
        super.onStop();
        C0668a.m8141b(C0668a.aW);
    }


    class C2024i implements C1154e {
        final /* synthetic */ UserInfoFragment f11500a;

        C2024i(UserInfoFragment c2023h) {
            this.f11500a = c2023h;
        }

        public void m10600a(DialogFragment dialogFragment) {
        }

        public void m10601b(DialogFragment dialogFragment) {
            dialogFragment.dismiss();
            C0668a.m8126a(this.f11500a.getActivity().getApplicationContext(), C0668a.gg);
            if (Utils.m8084k(this.f11500a.getActivity().getApplicationContext())) {
                new C2025j(this).execute(new Void[0]);
            } else {
                C3247b.m22930a(this.f11500a.getActivity().getApplicationContext(), this.f11500a.getString(R.string.please_connect_internet), 0).show();
            }
        }

        public void m10602c(DialogFragment dialogFragment) {
            dialogFragment.dismiss();
        }
    }

    class C2025j extends AsyncTask<Void, Void, Boolean> {
        final /* synthetic */ C2024i f11501a;

        C2025j(C2024i c2024i) {
            this.f11501a = c2024i;
        }

        protected Boolean m15877a(Void... voidArr) {
            if (this.f11501a.f11500a.mUserInfo != null) {
                GuliLog.tracePrintInfo("UserInfoFragment", "before del userinfo " + this.f11501a.f11500a.mUserInfo);
            }
            boolean b = this.f11501a.f11500a.m15873g();
            if (b) {
                WeightGoalManager.getInstance().deleteUserWeightGoalInLocal(this.f11501a.f11500a.mUserInfo.uid);
                WeightUtil.deleteUserWeights(this.f11501a.f11500a.getActivity(), this.f11501a.f11500a.mUserInfo.uid);
            }
            return Boolean.valueOf(b);
        }

        protected void m15878a(Boolean bool) {
            Utils.m7991a(this.f11501a.f11500a.getActivity());
            if (bool.booleanValue()) {
                GuliLog.tracePrintInfo("UserInfoFragment", "after del userinfo " + this.f11501a.f11500a.mUserInfo);
                EventBus.getDefault().post(new EventUserDeleted(this.f11501a.f11500a.mUserInfo.uid));
                this.f11501a.f11500a.getActivity().finish();
                C0668a.m8126a(this.f11501a.f11500a.getActivity(), C0668a.fr);
                return;
            }
            GuliLog.tracePrintInfo("UserInfoFragment", "onSyncFail");
            C3247b.m22930a(this.f11501a.f11500a.getActivity().getApplicationContext(), this.f11501a.f11500a.getString(R.string.please_connect_internet), 0).show();
            C0668a.m8126a(this.f11501a.f11500a.getActivity(), C0668a.fs);
        }

        protected /* synthetic */ Boolean doInBackground(Void[] objArr) {
            return m15877a((Void[]) objArr);
        }

        protected /* synthetic */ void onPostExecute(Boolean obj) {
            m15878a((Boolean) obj);
        }

        protected void onPreExecute() {
            super.onPreExecute();
            Utils.m7992a(this.f11501a.f11500a.getActivity(), (int) R.string.delete_family_member);
        }
    }

}
